/***************************************************************************
 *   Copyright (C) 2008 by Łukasz Spintzyk   *
 *   lukasz.spintzyk@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef T6963C_H
#define T6963C_H

 /*! This file contains functions which controls LCD Displays with toshiba6963 chip. 
  */
#include "device_opt.h"
#include <avr/io.h>
#include <inttypes.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <util/delay_basic.h>

//!definition of control lines in device_opt.h

#include "device_opt.h"


#define SETCURSORPTR 0x21
#define SETOFFSETREG 0x22
#define SETADDRESSPTR 0x24

#define SET_TH_ADDR 0x40
#define SET_TA 0x41
#define SET_GH_ADDR 0x42
#define SET_GA 0x43

//!--------------------COMBINE SETMODE WITH SUBMODES with "|" ------------
#define SETMODE 0x80

#define ORMODE 0x00
#define EXORMODE 0x01
#define ANDMODE 0x03
#define TEXTATTRMODE 0x04

#define INTERNALCGROM 0x00
#define EXTERNALCGRAM 0x08


//!--------------------COMBINE SETDISPAYMODE WITH SUBMODES with "|" ------------
#define SETDISPLAYMODE 0x90

#define DISPLAYOFF 0x90

#define CURSORBLINKOFF 0x02
#define CURSORBLINKON 0x03

#define TEXTONGRAPHICOFF 0x04
#define TEXTOFFGRAPHICON 0x08
#define TEXTONGRAPHICON 0x0C

//!---------------------ADD to SET_N_LINECURSOR number of lines, 1-8
#define SET_N_LINECURSOR 0xA0-0x01

#define SETAUTOWRITE 0xB0
#define SETAUTOREAD 0xB1
#define SETAUTORESET 0xB2


#define SETWRANDINC 0xC0
#define SETRDANDINC 0xC1
#define SETWRITEANDDEC 0xC2
#define SETRDANDDEC 0xC3
#define SETWRANDNONE 0xC4
#define SETRDANDNONE 0xC5

#define SCREENPEAK 0xE0
#define SCREENCOPY 0xE8

//!--------------------COMBINE NBITSET OR NBITRESET with "|" ------------
#define NBITSET 0xF8
#define NBITRESET 0xF0

extern uint16_t textHome;
extern uint16_t graphHome;


void send_graph_ram(const unsigned char buffer[][30]);

/*! Display ascii text from buffer on x,y position of TEXT AREA.
 * TEXT AREA is set by macrodefinition TA.
 * Fonts used to draw characters are taken from internal CG-ROM of lcd driver.
*/
void display_text(const char *buffer,uint8_t x,uint8_t y);

void display_text_PGM(PGM_P buffer,uint8_t x,uint8_t y);


/*! Initialization of LCD TEXT RAM AREA with zero
 * \param textArea - begin address(16bit) of text area in VRAM of LCD
 */
void init_text_ram(uint16_t textArea);

/*! Initialization of LCD GRAPHICS RAM AREA with fill patern
     * \param fill value wich fill the memory of LCD
     * \param graphArea - begin address(16bit) of text area in VRAM of LCD
 */
void init_graph_ram(char fill,uint16_t graphArea);

/*! Initialization procedure for LCD
 */
void lcd_init(uint16_t ptextHome, uint16_t pgraphHome,uint8_t textArea,uint8_t graphArea);

/*! Send command without argument
 */
void lcd_write_0cmd(unsigned char cmd);

/*! Send command with one argument
 */
void lcd_write_1cmd(unsigned char arg, unsigned char cmd);

/*! Send command with two arguments
 * \param arg1 Argument nr 1
 * \param arg2 Argument nr2
 * \param cmd Command code
 */
void lcd_write_2cmd(unsigned char arg1, unsigned char arg2, unsigned char cmd);

/*! Initialization of lcd controling lines and reset */
void lcd_hwd_init(void);

/*! Send data on bus.
 * \param data -8-bit value which will be send to LCD
 */
void wr_lcd_bus(uint8_t data);

/*! Read data from lcd bus
 */
uint8_t rd_lcd_bus(void);

/*! Write data to lcd_drive
 */
void lcd_write_data(unsigned char data);

/*! Read data from data bus
 * \return value read from databus
 */
unsigned char lcd_read_data(void);

/*! WRITE COMMAND FOR LCD_drive 
 * \param cmd - code of command
 */
void lcd_write_cmd(unsigned char cmd);
        
/*! Read status register from lcd drive.
 * line LCD_CD must set high
 * \return value of status register of LCD
 */
uint8_t lcd_read_status(void);

void delay (void);


/*! Writes custom character code to CG-RAM area
 * \param character -pointer to table of 8 bytes code
 * \param addr - addres of character in cg ram
 */
void init_CGRAM(char * character,uint16_t addr);

/*! Displays ram_char character from cgRam at position x,y
 */
void display_char(char ram_char,uint8_t x,uint8_t y);

#endif
